package li.nibble.twittertido.adapter;

import java.util.ArrayList;

import li.nibble.twittertido.R;
import li.nibble.twittertido.beans.Tweet;
import li.nibble.twittertido.lazyimage.ImageLoader;
import li.nibble.twittertido.model.DataBase;
import li.nibble.twittertido.webservice.Client;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class QueueAdapter extends ArrayAdapter<Tweet> {

	private ArrayList<Tweet> items;
	LayoutInflater vi;
	public ImageLoader imageLoader;

	public QueueAdapter(Context context, int textViewResourceId,
			ArrayList<Tweet> items) {
		super(context, textViewResourceId, items);
		this.items = items;
		vi = (LayoutInflater) getContext().getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
	}

	public static class ViewHolder {
		public TextView tweet;
		public TextView user;
		public ImageView image;
		public LinearLayout options;
	}

	public View getView(int position, final View convertView, ViewGroup parent) {
		View v = convertView;

		final ViewHolder holder;

		if (v == null) {
			v = vi.inflate(R.layout.queue_row, null);
			holder = new ViewHolder();
			holder.image = (ImageView) v.findViewById(R.id.avatar_user);
			holder.tweet = (TextView) v.findViewById(R.id.tweet);
			holder.user = (TextView) v.findViewById(R.id.user);
			holder.options = (LinearLayout) v.findViewById(R.id.tweet_options);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}

		final Tweet tweet = items.get(position);
		holder.tweet.setText(tweet.getTweet());
		holder.user.setText("@" + tweet.getUser());
		imageLoader.DisplayImage(
				"https://api.twitter.com/1/users/profile_image?screen_name="
						+ tweet.getUser() + "&size=normal", holder.image);

		if (tweet.isOptionsOpen()) {
			v.findViewById(R.id.tweet_options).setVisibility(View.VISIBLE);
		} else {
			v.findViewById(R.id.tweet_options).setVisibility(View.GONE);
		}

		v.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				openCloseOptions(
						(LinearLayout) v.findViewById(R.id.tweet_options),
						tweet);
			}
		});

		Button approve = (Button) v.findViewById(R.id.approve);
		Button reject = (Button) v.findViewById(R.id.reject);

		approve.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				vote(tweet, Tweet.LIKE);
			}
		});

		reject.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				vote(tweet, Tweet.DISLIKE);
			}
		});

		return v;
	}

	public void vote(Tweet tweet, int like) {
		Client.like(getContext(), tweet.getId(), like);
		DataBase.removeTweet(getContext(), tweet, DataBase.TABLE_QUEUE);
		items.remove(tweet);
		this.notifyDataSetChanged();
	}

	public void openCloseOptions(LinearLayout more_options, Tweet tweet) {
		if (more_options.getVisibility() == View.GONE) {
			more_options.setVisibility(View.VISIBLE);
			tweet.setOptionsOpen(true);
		} else {
			more_options.setVisibility(View.GONE);
			tweet.setOptionsOpen(false);
		}
	}

}
